We told you this was just the beginning of our game empire! Now that you know more about JavaScript, you'll be able to create a much richer "choose your own adventure" game.
We told you this was just the beginning of our game empire! Now that you know more about JavaScript, you'll be able to create a much richer "choose your own adventure" game.
First, we'll need to use a prompt
statement to ask our user what he or she wants to do. Recall that we use prompt like this:
prompt
in a variable so we can use the user's response to influence what the program doesYou may have noticed us use the.toUpperCase()
function in the first exercise. We used it like this:
This converted the user's answer to ALL CAPS before saving it in theanswer
variable. This helps eliminate problems that might crop up if your program tests for 'YES'
but your user typed in 'yes'
or 'Yes'
. The input becomes all caps before we test, so we only have to test for all caps!
You can also use .toLowerCase()
, which converts a string to all lower-case letters.
.toUpperCase()
or.toLowerCase()
on your prompt to ensure that the input you get from the user is capitalized the way you expect.Great work! Now let's get to the heart of our game: the switch
statement.
Good! Now let's spice things up with some logical operators.
Add some if
/else
statements to yourcase
s that check to see whether one condition and another condition are true, as well as whether one condition oranother condition are true. Use &&
and||
at least one time each.